package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 16/8/2.
 */
public class GetNewFriendListTask extends CouponHttpBaseTask{
    private int mPageNo;
    private int mSize;

    public GetNewFriendListTask(int pageno,int size) {
        super();
        mPageNo = pageno;
        mSize = size;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject respJson = response.getBody();
        int pageCount = 0;
        int currentPage = 0;
        int nextPage = 0;
        List<EatShowUserInfo> list = new ArrayList<EatShowUserInfo>();
        JSONObject jsonString = null;
        if (respJson != null) {
            jsonString = respJson.getJSONObject("studentList");
            currentPage = jsonString.getIntValue(ProtocolConst.CURRENT_PAGE);
            pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
            nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
            list = JSON.parseArray(jsonString.getString(ProtocolConst.LIST_TAG), EatShowUserInfo.class);
        }

        notifyTaskCompleted(nextPage, list);
    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT){
            CouponHttpRequest request = new CouponHttpRequest();
            request.setApiNo(ApiNo.NEWFRIENDLIST);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("size",mSize);
            jsonObject.put("pageNo",mPageNo);
            request.setData(jsonObject.toString());
            addRequest(request);
        }
    }
}
